package git.bds.nyc.applet.api.controller.mine;

import git.bds.nyc.applet.api.convert.DemandConvert;
import git.bds.nyc.applet.api.model.vo.demand.DemandVO;
import git.bds.nyc.applet.api.service.demand.DemandService;
import git.bds.nyc.demand.model.dto.DemandDTO;
import git.bds.nyc.demand.model.dto.DemandModifyDTO;
import git.bds.nyc.page.PageParam;
import git.bds.nyc.page.PageResult;
import git.bds.nyc.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author 成大事
 * @since 2023/4/22 16:51
 */
@Api(tags = "需求管理接口")
@Validated
@RestController
@RequestMapping("/applet/mine/demand")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DemandManageController {

    private final DemandService demandService;

    /**
     * 根据类型分页查看自己发布的需求记录
     *
     * @param pageParam 页面参数
     * @param type      审核状态(-1：未审核；0：不通过；1：审核通过)
     * @return {@link R}<{@link PageResult}<{@link DemandVO}>>
     */
    @ApiOperation(value = "根据类型分页查看自己发布的需求记录",notes = "分为审核状态(-1：未审核；0：不通过；1：审核通过)")
    @PostMapping("/getReleaseDemandPageByType/{type}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", dataTypeClass = Integer.class, paramType = "path", example = "1", required = true)
    })
    public R<PageResult<DemandVO>> getReleaseDemandPageByType(
            @RequestBody PageParam pageParam,
            @PathVariable("type") @Min(-1) @Max(1) int type
    ){
        PageResult<DemandDTO> page = demandService.getReleaseDemandPageByType(pageParam, type);
        List<DemandVO> demandVOS = DemandConvert.INSTANCE.toDemandVO(page.getList());
        return R.ok(new PageResult<>(demandVOS,page.getTotal()));
    }

    /**
     * 修改发布的需求信息
     *
     * @param demandModifyDTO 需求修改dto
     * @return {@link R}<{@link Boolean}>
     */
    @PostMapping("/modifyDemandInfo")
    @ApiOperation("修改发布的需求信息")
    public R<Boolean> modifyDemandInfo(
            @Validated @RequestBody DemandModifyDTO demandModifyDTO
    ){
        return R.decide(demandService.modifyDemandInfo(demandModifyDTO));
    }


    /**
     * 根据需求id删除需求
     *
     * @param id 需求id
     * @return {@link = R<Boolean>}
     */
    @ApiOperation("根据id删除需求数据")
    @DeleteMapping("/delDemandById/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需求id", dataTypeClass = Long.class, paramType = "path", example = "123456", required = true)
    })
    public R<Boolean> delDemandById(@PathVariable(value = "id") Long id) {
        return R.decide(demandService.delDemandById(id));
    }

}
